package com.lfh.lfhoss.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.comm.ResponseMessage;
import com.aliyun.oss.model.Callback;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.lfh.lfhoss.config.OssConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import sun.net.idn.StringPrep;

import java.io.*;

/**
 * @ProjectName: lfh-oss
 * @Package: com.lfh.lfhoss.controller
 * @ClassName: OssTest
 * @Author: lfh
 * @Description:
 * @Date: 2020/3/9 15:43
 * @Version: 1.0
 */
@Controller
public class OssController {

    @Autowired
    private OssConfig ossConfig;
    public void test1() throws IOException {

        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint =ossConfig.getEndpoint();

// 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
        String accessKeyId = ossConfig.getAccessKeyId();
        String accessKeySecret = ossConfig.getAccessKeySecret();
        String bucketName = ossConfig.getBucketName();
// <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径，例如abc/efg/123.jpg。
        String objectName = "image/0f26461e-7041-47f4-91d2-771cf7c52efd.jpg";

// 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// 上传内容到指定的存储空间（bucketName）并保存为指定的文件名称（objectName）。
        String content = "D:\\pic\\0f26461e-7041-47f4-91d2-771cf7c52efd.jpg";
        File file = new File("D:\\pic\\f01d810b-cb74-4da9-8d99-b082b554e8eb.jpg");
        FileInputStream fileInputStream = new FileInputStream(file);
        PutObjectResult putObjectResult = ossClient.putObject(bucketName, objectName,fileInputStream );
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName,new ByteArrayInputStream(content.getBytes()));
        String uri = putObjectResult.getResponse().getUri();
        String eTag = putObjectResult.getETag();




// 关闭OSSClient。
        ossClient.shutdown();
    }
}
